package com.neu.system.service.impl;

import com.neu.system.service.IMqttService;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.stereotype.Service;

@Service
public class MqttServiceImpl implements IMqttService {

    @Autowired
    private MqttPahoClientFactory mqttClientFactory;

    @Override
    public void sendCommand(String command) {
        try {
            MqttClient client = new MqttClient("tcp://47.109.89.8:1883", "SpringSender");
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName("root23");
            options.setPassword("root34".toCharArray());
            client.connect(options);

            MqttMessage message = new MqttMessage(command.getBytes());
            client.publish("SmarthomeSub123", message);
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
} 